import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login.vue'
import Blogs from '../views/Blogs.vue'
import Register from '../views/Register.vue'
import Error from '../views/404.vue'
import Blog from '../components/Blog.vue'
import BlogDetail from '../views/BlogDetail.vue'

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/',
      name: 'Index',
      redirect: '/blogs'
    },
    {
      path: '/blogs',
      name: 'Blogs',
      component: Blogs,
      children: [{
          path: '/blogs',
          component: Blog
        },
        {
          path: '/archives',
          component: Error
        },
        {
          path: '/tags',
          component: Error
        },
        {
          path: '/categories',
          component: Error
        },
        {
          path: '/link',
          component: Error
        },
        {
          path: '/msgboard',
          component: Error
        },
        {
          path: '/tool',
          component: Error
        },
        {
          path: '/write',
          component: Error
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/404',
      name: '404',
      component: Error
    },
    {
      path: '/blog/:blogId',
      name: 'BlogDetail',
      component: BlogDetail
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // if (to.path === '/login') return next()
  // // 获取token
  // const tokenStr = window.sessionStorage.getItem('token')
  // if (to.path === '/blogs') return next()
  // if (tokenStr) {
  //   return next('/login')
  // }
  next()
})
export default router
